{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8d7354cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4eca39e",
   "metadata": {},
   "source": [
    "## Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d317a681",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    Jack\n",
       "B    Lucy\n",
       "C     Tom\n",
       "Name: Name, dtype: object"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "names = pd.Series(['Jack', 'Lucy', 'Tom'], index=list('ABC'), name='Name', dtype=np.str_)\n",
    "display(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "903459a1-b1ea-4604-a217-7a0b1fd30ab2",
   "metadata": {},
   "source": [
    "### Series 如何索引\n",
    "#### 位置索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "129bca1d-9b67-4040-948e-e0737fe3199b",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/n2/fwhfgyqx3151x1fbxdrvm3rr0000gn/T/ipykernel_13239/1107863282.py:1: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  display(names[1])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Lucy'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'Lucy'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(names[1])\n",
    "\n",
    "display(names.iloc[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75497f0b-be03-4e80-b60f-7fc2dde17f11",
   "metadata": {},
   "source": [
    "#### 标签索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0a480ba3-d8ec-4c96-aa61-10a6973b67e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Lucy'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'Lucy'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(names['B'])\n",
    "\n",
    "display(names.loc['B'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a6802989-3d38-4e0c-9dd7-bdfc13adf2e8",
   "metadata": {},
   "source": [
    "### Series 如何切片\n",
    "#### 位置索引切片\n",
    "⁉️ 注意左闭右开"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b1aa0c61-6f48-4040-845c-0822f428ae94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    10\n",
       "B    20\n",
       "C    30\n",
       "D    40\n",
       "E    50\n",
       "F    60\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "employeeIds = pd.Series([10, 20, 30, 40, 50, 60], index=list('ABCDEF'))\n",
    "display(employeeIds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "bfd9cf43-5d79-41e5-92d0-b7799b51f134",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "B    20\n",
       "C    30\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "B    20\n",
       "C    30\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "A    10\n",
       "C    30\n",
       "E    50\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(employeeIds[1:3])\n",
    "\n",
    "display(employeeIds.iloc[1:3])\n",
    "\n",
    "# 设置步长\n",
    "display(employeeIds[::2])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a53099e9-c475-4cac-8d27-d35a46a3d972",
   "metadata": {},
   "source": [
    "#### 标签索引切片\n",
    "⁉️ 注意标切片，左闭右闭"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a87f7edf-83eb-4683-8ff2-08126b7bbf88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "B    20\n",
       "C    30\n",
       "D    40\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "B    20\n",
       "C    30\n",
       "D    40\n",
       "dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(employeeIds['B':'D'])\n",
    "\n",
    "display(employeeIds.loc['B':'D'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fbde1ad-9d5a-4360-9c27-4e851f80c1c6",
   "metadata": {},
   "source": [
    "### Series 有哪些属性？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "a6c46897-0cc5-488d-a6e4-4aa0db04b551",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    Jack\n",
       "B    Lucy\n",
       "C     Tom\n",
       "Name: Name, dtype: object"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'index'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "Index(['A', 'B', 'C'], dtype='object')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'values'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array(['Jack', 'Lucy', 'Tom'], dtype=object)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'dtype'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "dtype('O')"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'size'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'shape'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'ndim'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'name'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'Name'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "names = pd.Series(['Jack', 'Lucy', 'Tom'], index=list('ABC'), name='Name', dtype=np.str_)\n",
    "# display(names)\n",
    "\n",
    "# index\n",
    "display('index', names.index)\n",
    "\n",
    "# values\n",
    "display('values', names.values)\n",
    "\n",
    "# dtype\n",
    "display('dtype', names.dtype)\n",
    "\n",
    "# size\n",
    "display('size', names.size)\n",
    "\n",
    "# shape\n",
    "display('shape', names.shape)\n",
    "\n",
    "# ndim\n",
    "display('ndim', names.ndim)\n",
    "\n",
    "# name\n",
    "display('name', names.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8357f6c4-f36a-4ed6-b796-4fc25f53deab",
   "metadata": {},
   "source": [
    "### Series如何转变为 Dataframe? "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "036e88b3-6a88-4d53-878b-26ffe884348c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>Jack</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>Lucy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>Tom</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Name\n",
       "A  Jack\n",
       "B  Lucy\n",
       "C   Tom"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>Jack</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>Lucy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>Tom</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Name\n",
       "A  Jack\n",
       "B  Lucy\n",
       "C   Tom"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "names = pd.Series(['Jack', 'Lucy', 'Tom'], index=list('ABC'), name='Name', dtype=np.str_)\n",
    "# display(names)\n",
    "\n",
    "df1 = names.to_frame()\n",
    "display(df1)\n",
    "\n",
    "df2 = pd.DataFrame(names)\n",
    "display(df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85443945-e295-4d93-a764-e03439b46543",
   "metadata": {},
   "outputs": [],
   "source": [
    "#### Series 与 Dataframe 区别\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "f37aa601-7ae0-4402-949d-2a65cf29ebe5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'size'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'size'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'shape'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(3,)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'shape'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(3, 1)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'ndim'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "'ndim'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# size\n",
    "display('size', names.size)\n",
    "display('size', df1.size)\n",
    "\n",
    "# shape\n",
    "display('shape', names.shape)\n",
    "display('shape', df1.shape)\n",
    "\n",
    "# ndim\n",
    "display('ndim', names.ndim)\n",
    "display('ndim', df1.ndim)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
